//
//  WEXSearchView.m
//  microBlog_oc
//
//  Created by apple on 16/4/17.
//  Copyright © 2016年 Hill. All rights reserved.
//

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

#import "WEXSearchView.h"
#import "UIView+WEXFrame.h"

@interface WEXSearchView()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *tf_search;
@property (nonatomic, strong) UIButton *bt_cancel;
@property (nonatomic, strong) MASConstraint *trailingConstriaint;

@end

@implementation WEXSearchView

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
        
        
    }
    return self;
}

# pragma mark -
# pragma mark 设置搜索框界面
- (void)setupUI {
    //    实例化一个取消按钮
    UIButton *bt_cancel = [[UIButton alloc] init];
    self.bt_cancel = bt_cancel;
    
    [bt_cancel addTarget:self action:@selector(didClickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [bt_cancel setTitle:@"取消" forState:UIControlStateNormal];
    bt_cancel.titleLabel.font = [UIFont systemFontOfSize:14];
    [bt_cancel setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    [bt_cancel setBackgroundColor:[UIColor blueColor]];
    [self addSubview:bt_cancel];
    
    //    设置按钮的约束
    [self.bt_cancel makeConstraints:^(MASConstraintMaker *make) {
        
        make.trailing.top.bottom.equalTo(self).offset(0);
        
        make.width.equalTo(50);
        
    }];
    
    
//    实例化一个输入框
    UITextField *tf_search = [[UITextField alloc] init];
    self.tf_search = tf_search;
    
    tf_search.delegate = self;
    tf_search.placeholder = @"没有我搜索不到的东西";
    tf_search.borderStyle = UITextBorderStyleRoundedRect;
    [self addSubview:tf_search];
    
//    设置输入框约束
    [self.tf_search makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.top.bottom.equalTo(self).offset(0);
        
        make.trailing.offset(0);
        
    }];
    
//    实例化一个放大镜view
    UIImageView *searchGlass = [[UIImageView alloc] init];
    
    searchGlass.contentMode = UIViewContentModeCenter;
    
    searchGlass.width = 30;
    searchGlass.height = 30;
    
    searchGlass.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    
    tf_search.leftView = searchGlass;
    
    tf_search.leftViewMode = UITextFieldViewModeAlways;
    
//    tf_search.leftView
    
    
}

# pragma mark -
# pragma mark 实现取消按钮点击方法
- (void)didClickCancelButton:(UIButton *)button {
    
    //更新约束
    [self.tf_search updateConstraints:^(MASConstraintMaker *make) {
       
        make.trailing.offset(0);
        
    }];
    
    //动画更新界面
    [UIView animateWithDuration:2 animations:^{
       
        [self layoutIfNeeded];
        
        
    }];
    //取消第一响应者
    [self.tf_search resignFirstResponder];
}

# pragma mark -
# pragma mark 输入框开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
    //更新输入框约束
    [self.tf_search remakeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.top.bottom.equalTo(self).offset(0);
        
        make.trailing.offset(-60);
    }];

    //动画更新界面
    [UIView animateWithDuration:2 animations:^{
       
        [self layoutIfNeeded];
        
    }];
}

# pragma mark -
# pragma mark 布局子控件
-(void)layoutSubviews {
    
    [super layoutSubviews];
 

}

@end
